package com.my.selectcoursesystem.controller;

import com.my.selectcoursesystem.bean.Student;
import com.my.selectcoursesystem.bean.Teacher;
import com.my.selectcoursesystem.service.serviceImpl.StudentServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

@Controller
public class StudentController {
    @Autowired
    private StudentServiceImpl studentService;

    public StudentController() {
    }

    public StudentController(StudentServiceImpl studentService) {
        this.studentService = studentService;
    }

    public String getCookie(HttpServletRequest request, String name) {
        Cookie[] cookies = request.getCookies();//根据请求数据，找到cookie数组
        String value = "";
        if (null == cookies) {//如果没有cookie数组
            System.out.println("没有cookie");
            value = "";
        } else {
            for (Cookie cookie : cookies) {
                System.out.println("cookieName:" + cookie.getName() + ",cookieValue:" + cookie.getValue());
                if (cookie.getName().equals(name)) {
                    value = cookie.getValue();
                    break;
                }
            }
        }
        return value;
    }

    @RequestMapping("/mys/{sid}")
    public String goToMy(@PathVariable(value = "sid") String sid, Model model, HttpServletRequest request) {
        String sname = this.getCookie(request, "sname");
//        String sid = this.getCookie(request, "sid");
        model.addAttribute("sid", sid);
        model.addAttribute("sname", sname);
        Student student = this.studentService.getStudentBySid(sid);
        model.addAttribute("student", student);
        return "student/queryVitaStu";
    }

    @RequestMapping(value = "/modifys/{sid}")
    public String modifyPass(@PathVariable(value = "sid") String sid, Model model, HttpServletRequest request) {
        String sname = this.getCookie(request, "sname");
        model.addAttribute("sid", sid);
        model.addAttribute("sname", sname);
        Student student = this.studentService.getStudentBySid(sid);
        model.addAttribute("student", student);
        return "student/modityPwStu";
    }

    // 修改密码
    @RequestMapping(value = "/modifiedsPass", method = RequestMethod.PUT)
    public String modifiedsPass(String spassword, Model model, HttpServletRequest request) {
        String sname = this.getCookie(request, "sname");
        String sid = this.getCookie(request, "sid");
        model.addAttribute("sid", sid);
        model.addAttribute("sname", sname);
        Student student = this.studentService.getStudentBySid(sid);
        student.setSpassword(spassword);
        Boolean isMod = this.studentService.modifyStu(student);
        if (isMod) {
            System.out.println("========");
            model.addAttribute("msg", "修改成功!");
            model.addAttribute("u", "student");
            return "success";
        } else {
            model.addAttribute("msg", "修改失败!");
            model.addAttribute("u", "student");
            return "fail";
        }
    }
}
